package com.loan.market.util

import android.content.Context
import com.appsflyer.AppsFlyerConversionListener
import com.appsflyer.AppsFlyerLib
import com.loan.market.LoanApp.Companion.application
import com.loan.market.http.getHttpMap

fun Context.afEvent(event: String) {
    if (SPUtil.get<String>("afKey", "").isBlank()) return
    AppsFlyerLib.getInstance().logEvent(this, event, getHttpMap(0))
}

fun initAppFlyer() {
    val key = SPUtil.get<String>("afKey", "")
    if (key.isBlank()) return
    AppsFlyerLib.getInstance().init(key, object : AppsFlyerConversionListener {
        override fun onConversionDataSuccess(data: MutableMap<String, Any>?) {
        }

        override fun onConversionDataFail(error: String?) {
        }

        override fun onAppOpenAttribution(data: MutableMap<String, String>?) {
        }

        override fun onAttributionFailure(error: String?) {
        }
    }, application)
    AppsFlyerLib.getInstance().start(application)
}

fun getAfId(): String {
    if (SPUtil.get<String>("afKey", "").isBlank()) return ""
    return AppsFlyerLib.getInstance().getAppsFlyerUID(application) ?: ""
}